昨天的 Day 5,我們成功建立了第一個 REST API。
只要在瀏覽器輸入:
http://localhost:8080/hello
Spring Boot 就會回傳:
Hello! I'm your health assistant!
雖然成功讓智慧健康助理「開口說話」了,但馬上就發現一個問題:
不管是誰使用,它都只會講同一句話 XD
真正的系統當然不能永遠只回傳固定內容。
所以今天要往前一步,讓 Spring Boot 第一次接收使用者輸入的資料!
今天的目標是讓網址從:
/hello
進化成:
/hello?name=Hannie
然後讓系統回傳:
Hello, Hannie! Welcome to your health assistant!
昨天建立的 HelloController 大概長這樣:
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello() {
return "Hello! I'm your health assistant!";
}
}
這段程式最大的問題就是:
return "Hello! I'm your health assistant!";
內容已經直接寫死在程式裡。
所以不管使用者輸入什麼,最後得到的結果都一樣。
如果希望使用者可以把自己的名字傳進來,我們就需要認識今天的新東西:
@RequestParam
@RequestParam 可以用來取得使用者透過網址傳進來的參數。
例如:
http://localhost:8080/hello?name=Hannie
這個網址可以拆成:
/hello
以及:
?name=Hannie
其中:
name
是參數名稱,而:
Hannie
就是這次傳入的值。
簡單來說:
name = Hannie
接下來只要讓 Java 把這個值接起來,就可以拿去做其他處理。
首先加入:
import org.springframework.web.bind.annotation.RequestParam;
接著修改昨天的 /hello:
@GetMapping("/hello")
public String hello(@RequestParam String name) {
return "Hello, " + name + "! Welcome to your health assistant!";
}
完整程式會變成:
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello(@RequestParam String name) {
return "Hello, " + name + "! Welcome to your health assistant!";
}
}
重新執行 Spring Boot 之後,在瀏覽器輸入:
http://localhost:8080/hello?name=Hannie
就可以看到:
Hello, Hannie! Welcome to your health assistant!
成功!
這次回傳的內容終於不是完全寫死的了。
接下來可以試著把網址改成:
http://localhost:8080/hello?name=Tom
這時候結果就會變成:
Hello, Tom! Welcome to your health assistant!
再換成:
http://localhost:8080/hello?name=Amy
就會得到:
Hello, Amy! Welcome to your health assistant!
也就是說,現在程式已經可以根據使用者傳入的資料,產生不同的 Response。
整個流程大概是:
使用者輸入網址
↓
/hello?name=Hannie
↓
Spring Boot 收到 Request
↓
@RequestParam 取得 name
↓
name = "Hannie"
↓
Java 組合新的字串
↓
回傳結果
這也是我們第一次真正讓「使用者輸入」影響程式的執行結果。
這時候我又想到一個問題。
如果使用者只輸入:
http://localhost:8080/hello
沒有提供:
?name=Hannie
會發生什麼事?
因為目前的 @RequestParam 預設要求這個參數一定要存在,所以沒有輸入 name 時,Spring Boot 會認為 Request 缺少必要的參數。
但是對使用者來說,這樣好像不是很方便。
所以我們可以幫它設定一個預設值。
修改成:
@GetMapping("/hello")
public String hello(
@RequestParam(defaultValue = "User") String name) {
return "Hello, " + name + "! Welcome to your health assistant!";
}
這樣如果輸入:
http://localhost:8080/hello?name=Hannie
結果還是:
Hello, Hannie! Welcome to your health assistant!
但是如果只輸入:
http://localhost:8080/hello
系統就會使用預設值:
User
最後顯示:
Hello, User! Welcome to your health assistant!
這樣就算使用者沒有輸入名字,程式也還是可以正常運作。
做到這裡可能會覺得:
「所以今天就只是讓 Java 知道我的名字嗎?」
其實 @RequestParam 後面還可以拿來接收更多資料。
例如我們之後可能需要:
height
weight
甚至可以想像一個網址:
/bmi?height=162&weight=50
Java 收到:
height = 162
weight = 50
之後就可以利用這兩個數字進行 BMI 計算。
也就是:
使用者輸入資料
↓
@RequestParam
↓
Java 取得資料
↓
Java 進行計算
↓
回傳結果
所以今天雖然只用了 name,但其實是在為下一步的健康功能做準備。
Day 5 的程式:
@GetMapping("/hello")
public String hello() {
return "Hello! I'm your health assistant!";
}
只能回傳固定內容。
而今天:
@GetMapping("/hello")
public String hello(
@RequestParam(defaultValue = "User") String name) {
return "Hello, " + name + "! Welcome to your health assistant!";
}
已經可以接收使用者傳入的資料。
最大的差別就是:
Day 5
使用者
↓
Spring Boot
↓
固定 Response
變成:
Day 6
使用者輸入資料
↓
Spring Boot
↓
Java 取得資料
↓
根據資料處理
↓
不同的 Response
雖然只是很小的改變,但這其實是之後很多功能的基礎。
經過前六天,目前已經完成:
@GetMapping
@RequestParam
目前我們還沒有真正進入 AI。
但我覺得先把 Spring Boot 最基本的資料傳遞方式搞懂,之後串接 AI 時應該會比較容易理解資料到底是怎麼傳來傳去的。
今天第一次讓 Spring Boot 接收到使用者自己輸入的資料。
其中最重要的新東西就是:
@RequestParam
它可以幫助我們取得網址中的參數。
例如:
/hello?name=Hannie
Java 就可以取得:
Hannie
再根據這個資料產生不同的結果。
今天的健康助理終於不再只會說固定台詞了 XD
下一步,我想正式加入第一個真正跟「健康」有關的功能。